#include "NameScene.h"
#include "D3DInput.h"
#include "D3DApp.h"
#include "tchar.h"
#include "cstdio"
#include <fstream> 
#include <iostream>

NameScene::NameScene(IDirect3DTexture9* background):mFont(0){
	loadTexture();

	mBackTex = background;
	mBackgroundPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	mTransparentGroundPos = D3DXVECTOR3(8.0f, 8.0f, 0.0f);
	mBackgroundCenter = D3DXVECTOR3((float)1920/2, (float)1080/2, 0.0f);
	mScoreList.clear();
	count = 0.0f;

	D3DXFONT_DESC fontDesc;
	fontDesc.Height          = 60;
	fontDesc.Width           = 0;
	fontDesc.Weight          = 0;
	fontDesc.MipLevels       = 1;
	fontDesc.Italic          = false;
	fontDesc.CharSet         = DEFAULT_CHARSET;
	fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
	fontDesc.Quality         = ANTIALIASED_QUALITY;
	fontDesc.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;
	strcpy_s(fontDesc.FaceName, _T("Hobo Std"));

	HR(D3DXCreateFontIndirect(gDevice, &fontDesc, &mFont));
	mNewScore	= 0;
	mName		= "";
}

NameScene::~NameScene(){
	mBackTex = NULL;
	ReleaseCOM(mFont);
}

void NameScene::loadScore(){
	mScoreList.clear();
	if(checkFile("Score.cr")){
		std::ifstream file("Score.cr");
		std::string str; 
		while (std::getline(file, str))
		{
			mScoreList.push_back(str);
		}
		file.close();
	}
}

void NameScene::setScore(int score){
	mNewScore = score;
}

void NameScene::onLostDevice(){
	mFont->OnLostDevice();
}

void NameScene::onResetDevice(){
	mFont->OnResetDevice();
}

bool NameScene::checkFile (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    } else {
        return false;
    }   
}

SceneRS NameScene::update(float dt){
	//pos
	SceneRS rs;
	rs.ID = -1;
	rs.Info = "";
	D3DDEVICE_CREATION_PARAMETERS cparams;
	RECT rect;

	gDevice->GetCreationParameters(&cparams);
	GetWindowRect(cparams.hFocusWindow, &rect);

	mWidth = rect.right - rect.left;
	mHeight= rect.bottom - rect.top;

	if(count > 0.0f){
		count-=dt;
		if(count<= 0.0f){
			count = 0.0f;
		}

		return rs;
	}
	if(mName.length() <= 10){
		if(gInput->keyDown(DIK_A)){
			mName+="A";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_B)){
			mName+="B";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_C)){
			mName+="C";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_D)){
			mName+="D";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_E)){
			mName+="E";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_F)){
			mName+="F";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_G)){
			mName+="G";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_H)){
			mName+="H";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_H)){
			mName+="H";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_I)){
			mName+="I";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_J)){
			mName+="J";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_K)){
			mName+="K";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_L)){
			mName+="L";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_M)){
			mName+="M";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_N)){
			mName+="N";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_O)){
			mName+="O";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_P)){
			mName+="P";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_Q)){
			mName+="Q";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_R)){
			mName+="R";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_S)){
			mName+="S";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_T)){
			mName+="T";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_U)){
			mName+="U";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_V)){
			mName+="V";
			count = 0.2f;
		}
	
		if(gInput->keyDown(DIK_W)){
			mName+="W";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_X)){
			mName+="X";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_Y)){
			mName+="Y";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_Z)){
			mName+="Z";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_0)){
			mName+="0";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_1)){
			mName+="1";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_2)){
			mName+="2";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_3)){
			mName+="3";
			count = 0.2f;
		}
		if(gInput->keyDown(DIK_4)){
			mName+="4";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_5)){
			mName+="5";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_6)){
			mName+="6";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_7)){
			mName+="7";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_8)){
			mName+="8";
			count = 0.2f;
		}

		if(gInput->keyDown(DIK_9)){
			mName+="9";
			count = 0.2f;
		}
	}

	if(gInput->keyDown(DIK_RETURN)){
		count = 0.2f;
		if(mName == ""){
			
		}else{
			//save
			loadScore();
			int needswap = -1;
			
			for(unsigned int i = 0; i < mScoreList.size() ; i++){
				std::string tempstr = mScoreList[i];
				unsigned int index = mScoreList[i].find_last_of(" ");
				std::string src = mScoreList[i].substr(index +1);
				int tempScore = atoi(src.c_str());
				if(tempScore <= mNewScore){
					needswap = i;
					break;
				}
			}

			if(needswap == -1){
				mScoreList.push_back(mName + " " + std::to_string(mNewScore));
			}else{
				std::vector<std::string> temp(mScoreList);
				mScoreList.clear();
				for(unsigned int i = 0; i< temp.size(); i++){
					if(i == needswap){
						mScoreList.push_back(mName + " " + std::to_string(mNewScore));
					}
					mScoreList.push_back(temp[i]);
				}
			}

			std::ofstream outfile;
			outfile.open("Score.cr");
			for(unsigned int i = 0; i<mScoreList.size(); i++){
				outfile<<mScoreList[i]<<std::endl;
				if(i>= 4) break;
			}

			outfile.close();

			rs.ID = 0;
			rs.Info = "";
		}
	}

	return rs;
}

void NameScene::draw(ID3DXSprite* sprite){
	drawBackground(sprite);
	drawTransparentGround(sprite);
	std::string name = "";
	std::string score = "";
	static char buffer[256];

	sprintf_s(buffer, "ENTER YOUR NAME");
	RECT R = {mWidth/2-200, mHeight/2 -200, 0, 0};
	HR(mFont->DrawTextA(0,buffer,-1,&R,DT_NOCLIP,D3DCOLOR_XRGB(227,227,227)));

	sprintf_s(buffer, mName.c_str());
	RECT R1 = {mWidth/2-120, mHeight/2 -100, 0, 0};
	HR(mFont->DrawTextA(0,buffer,-1,&R1,DT_NOCLIP,D3DCOLOR_XRGB(139,233,69)));
	
}

void NameScene::drawBackground(ID3DXSprite* sprite){
	if(mBackTex == 0) return;

	float rate  = 1.0f;
	float wrate = (float)mWidth/1920;
	float hrate = (float)mHeight/1080;
	if(wrate > hrate){
		rate = wrate;
	}else{
		rate = hrate;
	}
	rate = ceilf(rate * 100) / 100; 
	rate = rate/2;
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,rate,-rate,1.0f);
	sprite->SetTransform(&(S));
	sprite->Draw(mBackTex,0,&mBackgroundCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	sprite->Flush();
}

void NameScene::drawTransparentGround(ID3DXSprite* sprite){
	if(mTex == 0) return;
	gDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
	D3DXMATRIX S;
	float rate = 100.0f;
	D3DXMatrixScaling(&S,25,-15, 0);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, 0);
	sprite->SetTransform(&(S*R));
	RECT rect;
	rect.left = 0;
	rect.right = 16;
	rect.top = 0;
	rect.bottom = 16;
	sprite->Draw(mTex,&rect,&mTransparentGroundPos,0,D3DCOLOR_ARGB(100, 0, 0, 0)); 
	sprite->Flush();
	gDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
}

void NameScene::loadTexture(){
	mTex = 0;
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/ScoreBackground.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
	}
}

void NameScene::releaseTexture(){
	if(mTex != 0){
		ReleaseCOM(mTex);
	}
}